<?php


namespace App\Service\common;


use App\Common\Res;
use App\Constants\ErrorCode;
use App\Service\client\accountService;
use App\Service\client\callBackService;
use App\Service\client\friendService;
use App\Service\client\taskService;
use App\Service\client\userService;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class switchService
{
    use Res;

    /**
     *解析传输类型
     * @param int $fd
     * @return array
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getSwitchService(int $fd, $string): array
    {
        var_dump($string);
        if (!empty($string)) {
            $dataArr = json_decode($string, true);
            if (!empty($dataArr) && !empty($dataArr['accessType'])) {
                var_dump('-------------请求accessType  ' . $dataArr['accessType']);
                $result = [];
                if ($dataArr['accessType'] != 'appLogin' && empty($dataArr['token'])) {
                    return $this->Error(ErrorCode::ERROR, '登录token不能为空');
                }
                switch ($dataArr['accessType']) {
                    /*设备相关*/
                    case'appLogin':
                        /*设备登录*/
                        $result = make(taskService::class)->appLogin($fd, $dataArr);
                        break;
                    case'appPop':
                        /*下发设备心跳*/
                        $result = make(taskService::class)->appPop($fd, $dataArr);
                        break;
                    case'appUploadFriendInfo':
                        /*上报采集信息*/
                        $result = make(userService::class)->appUploadFriendInfo($fd, $dataArr);
                        break;
                    case'appUploadAccountInfo':
                        /*客户端上报采集号信息*/
                        $result = make(accountService::class)->appUploadAccountInfo($fd, $dataArr);
                        break;
                    case'appUploadAccountError':
                        /*客户端上报采集号异常信息*/
                        $result = make(accountService::class)->appUploadAccountError($fd, $dataArr);
                        break;
                    /*--------------------------用户聊天相关--------------------------*/
                    case'appUploadTalkInfo':
                        /*客户端发送聊天信息给服务端*/
                        $result = make(friendService::class)->appUploadTalkInfo($fd, $dataArr);
                        break;
                    /*--------------------------接收消息处理--------------------------*/
                    case'appMessageToFriend':
                        /*消息回调接口*/
                        $result = make(callBackService::class)->appMessageToFriend($fd, $dataArr);
                        break;
                    /*--------------------------添加好友结果回调--------------------------*/
                    case'appAddFriendCallBack':
                        /*消息回调接口*/
                        $result = make(callBackService::class)->appAddFriendCallBack($fd, $dataArr);
                        break;
                    case'appAddFriend':
                        /*消息回调接口*/
                        $result = make(callBackService::class)->appAddFriend($fd, $dataArr);
                        break;
                    /*--------------------------检查最后一条消息类型--------------------------*/
                    case'appGetFriendLastMessage':
                        /*检查最后一条消息类型*/
                        $result = make(friendService::class)->appGetFriendLastMessage($fd, $dataArr);
                        break;
                }
                return $result;
            } else {
                return $this->Error(ErrorCode::ERROR, '消息解析失败' . $string);
            }
        }
        return $this->Error(ErrorCode::ERROR, '消息数据错误' . $string);
    }
}